package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.annotation.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class AlbumInfoApiController {
    @Resource
    private AlbumInfoService albumInfoService;

    @Login
    @PostMapping("saveAlbumInfo")
    @Operation(summary = "保存专辑信息")
    public Result saveAlbumInfo(@Validated @RequestBody AlbumInfoVo albumInfoVo) {
        this.albumInfoService.saveAlbumInfo(albumInfoVo);
        return Result.ok();
    }
    @Login
    @PostMapping("findUserAlbumPage/{page}/{limit}")
    @Operation(summary = "分页查询专辑信息")
    public Result<IPage<AlbumListVo>> findUserAlbumPage(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody AlbumInfoQuery albumInfoQuery) {
        IPage<AlbumListVo> iPage = new Page<>(page, limit);
        IPage<AlbumListVo> albumListVoIPage = this.albumInfoService.findUserAlbumPage(iPage, albumInfoQuery);
        return Result.ok(albumListVoIPage);
    }
    @Login
    @DeleteMapping("removeAlbumInfo/{id}")
    @Operation(summary = "根据id删除专辑信息")
    public Result removeAlbumInfo(@PathVariable Long id) {
        this.albumInfoService.removeAlbumInfo(id);
        return Result.ok();
    }

    @GetMapping("getAlbumInfo/{id}")
    @Operation(summary = "根据id获取专辑信息")
    public Result<AlbumInfo> getAlbumInfo(@PathVariable Long id) {
        AlbumInfo albumInfo = this.albumInfoService.getAlbumInfo(id);
        return Result.ok(albumInfo);
    }
    @Login
    @PutMapping("updateAlbumInfo/{id}")
    @Operation(summary = "根据id更新专辑信息")
    public Result updateAlbumInfo(@PathVariable Long id, @RequestBody AlbumInfoVo albumInfoVo) {
        this.albumInfoService.updateAlbumInfo(id, albumInfoVo);
        return Result.ok();
    }
    @Login
    @GetMapping("findUserAllAlbumList")
    @Operation(summary = "获取用户所有专辑信息")
    public Result<List<AlbumInfo>> findUserAllAlbumList() {
        Long userId = AuthContextHolder.getUserId() == null ? 1 : AuthContextHolder.getUserId();
        List<AlbumInfo> albumInfoList = this.albumInfoService.findUserAllAlbumList(userId);
        return Result.ok(albumInfoList);
    }


    @PostMapping("findUserAlbum/{albumId}")
    @Operation(summary = "根据id查询专辑信息")
    public Result<AlbumListVo> findUserAlbum(@PathVariable Long albumId) {
        AlbumListVo albumListVo= this.albumInfoService.findUserAlbum(albumId);
        return Result.ok(albumListVo);
    }

    @PostMapping("findAllAlbumPage/{page}/{limit}")
    @Operation(summary = "分页查询专辑信息")
    public Result<List<AlbumListVo>> findAllAlbumPage(@PathVariable Integer page, @PathVariable Integer limit) {
        List<AlbumListVo> albumListVos = this.albumInfoService.findAllAlbumPage(page,limit);
        return Result.ok(albumListVos);
    }

    @GetMapping("getChangeAlbum/{startTime}/{endTime}")
    public Result<List<Long>> getChangeAlbum(@PathVariable String startTime , @PathVariable String endTime){
        List<Long> albumIds = this.albumInfoService.getChangeAlbum(startTime,endTime);
        return Result.ok(albumIds);
    }

    @PostMapping("getChangeAlbumStats")
    public Result<List<AlbumStatVo>> getChangeAlbumStats(@RequestBody List<Long> albumIds){
        List<AlbumStatVo> albumStatVos = this.albumInfoService.getChangeAlbumStats(albumIds);
        return Result.ok(albumStatVos);
    }
}

